﻿/*
 *  Source code courtesy of the desktopWeb.CodePlex.com community project. See MS-PL license on Codeplex.com. 
 *  This is beta code not intended for a production application.
 */

//NEXT: Move to MyData.Common/ExpressionForms
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyData.ExpressionBuilder.Interfaces;
using System.Collections.ObjectModel;
using MyData.ExpressionBuilder.Model;
using System.Activities.Presentation.Model;
using System.Diagnostics;

namespace MyData.Extensions
{
  public partial class SimpleExpressionForm : Form
  {
    Action<object> _onUpdated;

    private ObservableCollection<ListItem> _result = new ObservableCollection<ListItem>();

    //Next: Pass the ViewModel to the Form. Form XAML control should bind to the ViewModel
    public SimpleExpressionForm(IEnumerable<ListItem> metadata, Action<object> onUpdated)
    {
      System.Windows.Forms.Application.EnableVisualStyles();
      InitializeComponent();

      if (metadata != null)
      {
        ModelItem mi = (from m in metadata where m.Name == "ModelItem" select m.Value).First() as ModelItem;
        string path = (from m in metadata where m.Name == "Path" select m.Value).First() as string;

        if (mi != null)
          if (mi.TargetExpression(path) != null)
            valueTextBox.Text = mi.TargetExpression(path).ToString().QuotedValue();

        this.Text += " for " + path;
      }

      _onUpdated = onUpdated;
    }

    public ObservableCollection<ListItem> Result
    {
      get { return this._result; }
    }

    private void okButton_Click(object sender, EventArgs e)
    {
      //Converter not used for simple quoted string
      _onUpdated(valueTextBox.Text);

      this.Close();
    }

    private void cancelButton_Click(object sender, EventArgs e)
    {
      this.Close();
    }

  }
}
